package com.zl.smartmall.library.vo;

import java.util.ArrayList;
import java.util.List;





import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.po.SearchRecord;

import net.tsz.afinal.FinalDb;
import android.content.Context;

public class SearchRecordDao {
	
	private FinalDb mFinalDb;
	public SearchRecordDao(Context context){
		mFinalDb = FinalDb.create(context);
	}
	
	public List<SearchRecord> getRecordList(){
//		List<SearchRecord> list = mFinalDb.findAll(SearchRecord.class, "id desc");
		List<SearchRecord> list = mFinalDb.findAllByWhere(SearchRecord.class, "currentUID="+IAuth.getInstance().loadUserInfo().getUid(), "id desc");
		if(list != null){
			if(list.size() >10){
				//删除部分
				final int id = list.get(10).getId();
				ThreadExecutor.getInstance().execute(new Runnable() {

					@Override
					public void run() {
//						 mFinalDb.deleteByWhere(SearchRecord.class, "id <"+id);
						 mFinalDb.deleteByWhere(SearchRecord.class, "id <"+id+" and currentUID="+IAuth.getInstance().loadUserInfo().getUid());
					}
				});
				return list.subList(0, 10);
			}
		}
		
		return list;
	}
	
	public void insert(final SearchRecord record){
		ThreadExecutor.getInstance().execute(new Runnable() {

			@Override
			public void run() {
				 mFinalDb.deleteByWhere(SearchRecord.class, "keyWord="+"'"+record.getKeyWord()+"'"+" and currentUID="+IAuth.getInstance().loadUserInfo().getUid());
				 mFinalDb.save(record);
			}
		});
		
	}
	
}
